﻿using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using PackagingTool.Helpers;
using PackagingTool.ViewModels;

namespace PackagingTool.Views;

public partial class BuildProjectSetting : Window
{
    public BuildProjectSetting()
    {
        InitializeComponent();
        DragControlHelper.StartDrag(Header);
    }

    protected BuildProjectSettingViewModel ViewModel => (BuildProjectSettingViewModel)DataContext;

    private void Close_OnClick(object? sender, RoutedEventArgs e)
    {
        Close();
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);

        DragControlHelper.StopDrag(Header);
    }

    private async void SelectBuild_OnClick(object? sender, RoutedEventArgs e)
    {
        var sp = GetStorageProvider();
        if (sp is null) return;
        var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
        {
            Title = "Select Folder",
            AllowMultiple = false,
        });

        if (result.Count == 1)
        {
            ViewModel.ProjectOutputPath = result[0].Path.AbsolutePath;
        }
    }


    private IStorageProvider? GetStorageProvider()
    {
        var topLevel = TopLevel.GetTopLevel(this);
        return topLevel?.StorageProvider;
    }

    private void Ok_OnClick(object? sender, RoutedEventArgs e)
    {
        Close(ViewModel);
    }
}